package com.easemob.chat.core;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import com.easemob.chat.EMChat;
import com.easemob.chat.EMChatManager;
import com.easemob.chat.EMContact;
import com.easemob.chat.EMConversation;
import com.easemob.chat.EMGroup;
import com.easemob.chat.EMMessage;
import com.easemob.chat.EMMessage.ChatType;
import com.easemob.chat.EMMessage.Direct;
import com.easemob.chat.EMMessage.Status;
import com.easemob.chat.MessageEncoder;
import com.easemob.util.EMLog;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

public class e
{
  private static String d = "chatdb";
  public static final String a = "_emmsg.db";
  private static final int e = 10;
  private static final String f = "_id";
  private static final String g = "msgid";
  private static final String h = "msgtime";
  private static final String i = "msgdir";
  private static final String j = "participant";
  public static final String b = "msgbody";
  private static final String k = "groupname";
  private static final String l = "isacked";
  private static final String m = "isdelivered";
  public static final String c = "status";
  private static final String n = "islistened";
  private static final String o = "chat";
  private static final String p = "emgroup";
  private static final String q = "name";
  private static final String r = "nick";
  private static final String s = "desc";
  private static final String t = "owner";
  private static final String u = "members";
  private static final String v = "members_size";
  private static final String w = "modifiedtime";
  private static final String x = "jid";
  private static final String y = "ispublic";
  private static final String z = "isblocked";
  private static final String A = "max_users";
  private static final String B = "unreadcount";
  private static final String C = "username";
  private static final String D = "count";
  private static final String E = "token";
  private static final String F = "username";
  private static final String G = "value";
  private static final String H = "saved_time";
  private static final String I = "contact";
  private static final String J = "jid";
  private static final String K = "username";
  private static final String L = "nick";
  private static final String M = "black_list";
  private static final String N = "username";
  private static final String O = "conversation_list";
  private static final String P = "username";
  private static final String Q = "groupname";
  private static final String R = "ext";
  private static final String S = "create table chat (_id integer primary key autoincrement, msgid text, msgtime integer, msgdir integer, isacked integer, isdelivered integer, status integer,participant text not null, islistened integer, msgbody text not null,groupname text);";
  private static final String T = "create table emgroup (name text primary key, jid text not null, nick text not null, owner text not null, modifiedtime integer, ispublic integer, desc text, members_size integer, isblocked integer, members text, max_users integer);";
  private static final String U = "create table unreadcount (username text primary key, count integer);";
  private static final String V = "create table token (username text primary key, value text, saved_time integer);";
  private static final String W = "create table contact (jid text primary key, username text, nick );";
  private static final String X = "create table black_list (username text primary key);";
  private static final String Y = "create table if not exists conversation_list (username text primary key, groupname text, ext text);";
  private static e Z = null;
  private String aa = null;
  private Context ab;
  private boolean ac = false;

  public static synchronized void a(String paramString)
  {
    EMLog.e(d, "initDB : " + paramString);
    if (Z != null)
    {
      if ((Z.aa != null) && (Z.aa.equals(paramString)))
        return;
      Z.b();
    }
    if (Z == null)
    {
      Z = new e();
      Z.ab = EMChat.getInstance().getAppContext();
    }
    Z.aa = paramString;
  }

  public static synchronized e a()
  {
    if (Z == null)
    {
      EMLog.e(d, "Please login first!");
      throw new IllegalStateException("Please login first!");
    }
    return Z;
  }

  public synchronized void b()
  {
    try
    {
      a.closeDB();
      EMLog.d(d, "close msg db");
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
  }

  private void l()
  {
    SQLiteDatabase localSQLiteDatabase = a.getInstance(this.ab, this.aa).getWritableDatabase();
    try
    {
      localSQLiteDatabase.execSQL("drop table chat");
      localSQLiteDatabase.execSQL("drop table emgroup");
      localSQLiteDatabase.execSQL("drop table unreadcount");
      localSQLiteDatabase.execSQL("drop table token");
      localSQLiteDatabase.execSQL("drop table contact");
      localSQLiteDatabase.execSQL("drop table black_list");
      localSQLiteDatabase.execSQL("drop table conversation_list");
      localSQLiteDatabase.execSQL("create table chat (_id integer primary key autoincrement, msgid text, msgtime integer, msgdir integer, isacked integer, isdelivered integer, status integer,participant text not null, islistened integer, msgbody text not null,groupname text);");
      localSQLiteDatabase.execSQL("create table emgroup (name text primary key, jid text not null, nick text not null, owner text not null, modifiedtime integer, ispublic integer, desc text, members_size integer, isblocked integer, members text, max_users integer);");
      localSQLiteDatabase.execSQL("create table unreadcount (username text primary key, count integer);");
      localSQLiteDatabase.execSQL("create table token (username text primary key, value text, saved_time integer);");
      localSQLiteDatabase.execSQL("create table contact (jid text primary key, username text, nick );");
      localSQLiteDatabase.execSQL("create table black_list (username text primary key);");
      localSQLiteDatabase.execSQL("create table if not exists conversation_list (username text primary key, groupname text, ext text);");
    }
    catch (Exception localException)
    {
    }
  }

  private SQLiteDatabase m()
  {
    return a.getInstance(this.ab, this.aa).getWritableDatabase();
  }

  public boolean a(EMMessage paramEMMessage)
  {
    try
    {
      SQLiteDatabase localSQLiteDatabase = a.getInstance(this.ab, this.aa).getWritableDatabase();
      ContentValues localContentValues = new ContentValues();
      localContentValues.put("msgid", paramEMMessage.getMsgId());
      localContentValues.put("msgtime", Long.valueOf(paramEMMessage.getMsgTime()));
      localContentValues.put("isacked", Boolean.valueOf(paramEMMessage.isAcked));
      localContentValues.put("isdelivered", Boolean.valueOf(paramEMMessage.isDelivered));
      localContentValues.put("msgdir", Integer.valueOf(paramEMMessage.direct.ordinal()));
      EMMessage.Status localStatus = paramEMMessage.status;
      if (localStatus == EMMessage.Status.INPROGRESS)
        localStatus = EMMessage.Status.CREATE;
      localContentValues.put("status", Integer.valueOf(localStatus.ordinal()));
      String str1;
      if (paramEMMessage.getFrom().equals(this.aa))
        str1 = paramEMMessage.getTo();
      else
        str1 = paramEMMessage.getFrom();
      localContentValues.put("participant", str1);
      localContentValues.put("msgbody", MessageEncoder.getJSONMsg(paramEMMessage, true));
      if (paramEMMessage.getChatType() == EMMessage.ChatType.GroupChat)
        localContentValues.put("groupname", paramEMMessage.getTo());
      else
        localContentValues.putNull("groupname");
      localContentValues.put("islistened", Integer.valueOf(paramEMMessage.isListened() ? 1 : 0));
      if (!str1.equals("bot"))
        localSQLiteDatabase.insert("chat", null, localContentValues);
      boolean bool = paramEMMessage.getChatType() == EMMessage.ChatType.GroupChat;
      String str2 = !bool ? str1 : (String)localContentValues.get("groupname");
      b(str2, bool);
      EMLog.d(d, "save msg to db");
      return true;
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
      EMLog.e(d, "save msg has error: " + localException);
    }
    return false;
  }

  public boolean b(EMMessage paramEMMessage)
  {
    try
    {
      SQLiteDatabase localSQLiteDatabase = a.getInstance(this.ab, this.aa).getWritableDatabase();
      ContentValues localContentValues = new ContentValues();
      String str1 = paramEMMessage.getMsgId();
      String str2 = MessageEncoder.getJSONMsg(paramEMMessage, true);
      localContentValues.put("msgbody", str2);
      localSQLiteDatabase.update("chat", localContentValues, "msgid = ?", new String[] { str1 });
      EMLog.d(d, "update msg:" + str1 + " messagebody:" + str2);
      return true;
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
    return false;
  }

  public void b(String paramString)
  {
    try
    {
      SQLiteDatabase localSQLiteDatabase = a.getInstance(this.ab, this.aa).getWritableDatabase();
      String str = "";
      boolean bool = false;
      Cursor localCursor = localSQLiteDatabase.rawQuery("select participant, groupname from chat where msgid = ? limit 1", new String[] { paramString });
      if (!localCursor.moveToFirst())
      {
        localCursor.close();
        return;
      }
      if (localCursor.getString(1) == null)
      {
        str = localCursor.getString(0);
        bool = false;
      }
      else
      {
        str = localCursor.getString(1);
        bool = true;
      }
      localCursor.close();
      int i1 = localSQLiteDatabase.delete("chat", "msgid = ?", new String[] { paramString });
      EMLog.d(d, "delete msg:" + paramString + " return:" + i1);
      c(str, bool);
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
  }

  private String a(Cursor paramCursor)
  {
    if (paramCursor == null)
      return "";
    int i1 = paramCursor.isNull(paramCursor.getColumnIndex("groupname")) ? 0 : 1;
    String str = i1 == 0 ? paramCursor.getString(paramCursor.getColumnIndex("participant")) : paramCursor.getString(paramCursor.getColumnIndex("groupname"));
    return str;
  }

  public Hashtable<String, EMConversation> c()
  {
    Hashtable localHashtable = new Hashtable();
    try
    {
      SQLiteDatabase localSQLiteDatabase = a.getInstance(this.ab, this.aa).getWritableDatabase();
      String str1 = "SELECT *, MAX(msgtime), COUNT(*) AS msgCount FROM chat AS A  where A.groupname is null and A.participant in (select username from conversation_list) GROUP BY participant ";
      String str2 = "SELECT *, MAX(msgtime), COUNT(*) AS msgCount FROM chat AS A  where A.groupname is not null and A.groupname in (select groupname from conversation_list) GROUP BY groupname ";
      String[] arrayOfString1 = { str1, str2 };
      for (String str3 : arrayOfString1)
      {
        Cursor localCursor = localSQLiteDatabase.rawQuery(str3, null);
        if (!localCursor.moveToFirst())
        {
          localCursor.close();
        }
        else
        {
          do
          {
            EMMessage localEMMessage = b(localCursor);
            long l1 = localCursor.getLong(localCursor.getColumnIndex("msgCount"));
            LinkedList localLinkedList = new LinkedList();
            localLinkedList.add(localEMMessage);
            boolean bool = !localCursor.isNull(localCursor.getColumnIndex("groupname"));
            String str4 = a(localCursor);
            EMConversation localEMConversation = new EMConversation(str4, localLinkedList, bool, Long.valueOf(l1));
            localHashtable.put(str4, localEMConversation);
          }
          while (localCursor.moveToNext());
          localCursor.close();
        }
      }
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
    return localHashtable;
  }

  public Hashtable<String, EMConversation> a(int paramInt)
  {
    Hashtable localHashtable = new Hashtable();
    EMMessage localEMMessage = null;
    try
    {
      SQLiteDatabase localSQLiteDatabase = a.getInstance(this.ab, this.aa).getWritableDatabase();
      String str1 = "select * from chat where groupname is null and participant in (select username from conversation_list) order by participant, msgtime desc";
      String str2 = "select * from chat where groupname is not null and groupname in (select groupname from conversation_list) order by groupname, msgtime desc";
      String[] arrayOfString1 = { str1, str2 };
      for (String str3 : arrayOfString1)
      {
        Cursor localCursor = localSQLiteDatabase.rawQuery(str3, null);
        if (!localCursor.moveToFirst())
        {
          localCursor.close();
        }
        else
        {
          Object localObject1 = null;
          Object localObject2 = null;
          long l1 = 0L;
          boolean bool = false;
          Object localObject3;
          do
          {
            localObject3 = a(localCursor);
            if ((localObject2 != null) && (((String)localObject2).equals(localObject3)) && (((List)localObject1).size() < paramInt))
            {
              localEMMessage = b(localCursor);
              ((List)localObject1).add(localEMMessage);
              l1 += 1L;
            }
            else if ((localObject2 == null) || (!((String)localObject2).equals(localObject3)))
            {
              if (localObject2 != null)
              {
                Collections.reverse((List)localObject1);
                EMConversation localEMConversation = new EMConversation((String)localObject2, (List)localObject1, bool, Long.valueOf(l1));
                localHashtable.put(localObject2, localEMConversation);
              }
              localObject1 = new LinkedList();
              ((List)localObject1).add(b(localCursor));
              bool = !localCursor.isNull(localCursor.getColumnIndex("groupname"));
              localObject2 = localObject3;
              l1 = 1L;
            }
          }
          while (localCursor.moveToNext());
          if (localObject2 != null)
          {
            Collections.reverse((List)localObject1);
            localObject3 = new EMConversation((String)localObject2, (List)localObject1, bool, Long.valueOf(l1));
            localHashtable.put(localObject2, localObject3);
          }
          localCursor.close();
        }
      }
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
    return localHashtable;
  }

  public long a(String paramString, boolean paramBoolean)
  {
    SQLiteDatabase localSQLiteDatabase = a.getInstance(this.ab, this.aa).getWritableDatabase();
    try
    {
      String str = !paramBoolean ? "participant" : "groupname";
      Cursor localCursor = localSQLiteDatabase.rawQuery("select count(*) as msgCount from chat where " + str + " = ?", new String[] { paramString });
      if (!localCursor.moveToFirst())
      {
        localCursor.close();
        return 0L;
      }
      long l1 = localCursor.getLong(0);
      localCursor.close();
      return l1;
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
    return 0L;
  }

  public EMMessage c(String paramString)
  {
    EMMessage localEMMessage = null;
    try
    {
      SQLiteDatabase localSQLiteDatabase = a.getInstance(this.ab, this.aa).getWritableDatabase();
      Cursor localCursor = localSQLiteDatabase.rawQuery("select * from chat where msgid = ?", new String[] { paramString });
      if (!localCursor.moveToFirst())
      {
        localCursor.close();
        return localEMMessage;
      }
      localEMMessage = b(localCursor);
      localCursor.close();
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
    EMLog.d(d, "load msg msgId:" + paramString);
    return localEMMessage;
  }

  public List<String> d()
  {
    ArrayList localArrayList = new ArrayList();
    try
    {
      SQLiteDatabase localSQLiteDatabase = a.getInstance(this.ab, this.aa).getWritableDatabase();
      Cursor localCursor = localSQLiteDatabase.rawQuery("select distinct username from conversation_list where groupname is null", null);
      if (!localCursor.moveToFirst())
      {
        localCursor.close();
        return localArrayList;
      }
      do
        localArrayList.add(localCursor.getString(0));
      while (localCursor.moveToNext());
      localCursor.close();
      EMLog.d(d, "load participants size:" + localArrayList.size());
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
    return localArrayList;
  }

  public List<String> e()
  {
    ArrayList localArrayList = new ArrayList();
    try
    {
      SQLiteDatabase localSQLiteDatabase = a.getInstance(this.ab, this.aa).getWritableDatabase();
      Cursor localCursor = localSQLiteDatabase.rawQuery("select distinct groupname from conversation_list where username is null", null);
      if (!localCursor.moveToFirst())
      {
        localCursor.close();
        return localArrayList;
      }
      do
        localArrayList.add(localCursor.getString(0));
      while (localCursor.moveToNext());
      localCursor.close();
      EMLog.d(d, "load msg groups size:" + localArrayList.size());
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
    return localArrayList;
  }

  public List<EMMessage> d(String paramString)
  {
    ArrayList localArrayList = new ArrayList();
    try
    {
      SQLiteDatabase localSQLiteDatabase = a.getInstance(this.ab, this.aa).getWritableDatabase();
      Cursor localCursor = localSQLiteDatabase.rawQuery("select * from chat where groupname = ? order by msgtime", new String[] { paramString });
      if (!localCursor.moveToFirst())
      {
        localCursor.close();
        return localArrayList;
      }
      do
      {
        EMMessage localEMMessage = b(localCursor);
        localArrayList.add(localEMMessage);
      }
      while (localCursor.moveToNext());
      localCursor.close();
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
    EMLog.d(d, "load msgs size:" + localArrayList.size() + " for group:" + paramString);
    return localArrayList;
  }

  public List<EMMessage> a(String paramString1, String paramString2, int paramInt)
  {
    ArrayList localArrayList = new ArrayList();
    try
    {
      SQLiteDatabase localSQLiteDatabase = a.getInstance(this.ab, this.aa).getWritableDatabase();
      Cursor localCursor = null;
      EMMessage localEMMessage;
      if (paramString2 != null)
      {
        localEMMessage = EMChatManager.getInstance().getMessage(paramString2);
        if (localEMMessage == null)
        {
          EMLog.e(d, "can't find message for startMsgId");
          return localArrayList;
        }
        localCursor = localSQLiteDatabase.rawQuery("select * from chat where groupname = ? and msgtime < ? order by msgtime desc limit ?", new String[] { paramString1, localEMMessage.getMsgTime(), paramInt });
      }
      else
      {
        localCursor = localSQLiteDatabase.rawQuery("select * from chat where groupname = ? order by msgtime desc limit ?", new String[] { paramString1, paramInt });
      }
      if (!localCursor.moveToLast())
      {
        localCursor.close();
        return localArrayList;
      }
      do
      {
        localEMMessage = b(localCursor);
        localArrayList.add(localEMMessage);
      }
      while (localCursor.moveToPrevious());
      localCursor.close();
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
    EMLog.d(d, "load msgs size:" + localArrayList.size() + " for groupid:" + paramString1);
    return localArrayList;
  }

  public List<EMMessage> e(String paramString)
  {
    ArrayList localArrayList = new ArrayList();
    try
    {
      SQLiteDatabase localSQLiteDatabase = a.getInstance(this.ab, this.aa).getWritableDatabase();
      Cursor localCursor = localSQLiteDatabase.rawQuery("select * from chat where participant = ? and groupname = null order by msgtime", new String[] { paramString });
      if (!localCursor.moveToFirst())
      {
        localCursor.close();
        return localArrayList;
      }
      do
      {
        EMMessage localEMMessage = b(localCursor);
        localArrayList.add(localEMMessage);
      }
      while (localCursor.moveToNext());
      localCursor.close();
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
    EMLog.d(d, "load msgs size:" + localArrayList.size() + " for participate:" + paramString);
    return localArrayList;
  }

  public List<EMMessage> b(String paramString1, String paramString2, int paramInt)
  {
    ArrayList localArrayList = new ArrayList();
    try
    {
      SQLiteDatabase localSQLiteDatabase = a.getInstance(this.ab, this.aa).getWritableDatabase();
      Cursor localCursor = null;
      EMMessage localEMMessage;
      if (paramString2 != null)
      {
        localEMMessage = EMChatManager.getInstance().getMessage(paramString2);
        if (localEMMessage == null)
        {
          EMLog.e(d, "can't find message for startMsgId");
          return localArrayList;
        }
        localCursor = localSQLiteDatabase.rawQuery("select * from chat where participant = ? and msgtime < ? and groupname is null order by msgtime desc limit ?", new String[] { paramString1, localEMMessage.getMsgTime(), paramInt });
      }
      else
      {
        localCursor = localSQLiteDatabase.rawQuery("select * from chat where participant = ? and groupname is null order by msgtime desc limit ?", new String[] { paramString1, paramInt });
      }
      if (!localCursor.moveToLast())
      {
        localCursor.close();
        return localArrayList;
      }
      do
      {
        localEMMessage = b(localCursor);
        localArrayList.add(localEMMessage);
      }
      while (localCursor.moveToPrevious());
      localCursor.close();
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
    EMLog.d(d, "load msgs size:" + localArrayList.size() + " for participate:" + paramString1);
    return localArrayList;
  }

  private EMMessage b(Cursor paramCursor)
  {
    String str1 = paramCursor.getString(paramCursor.getColumnIndex("msgbody"));
    EMMessage localEMMessage = MessageEncoder.getMsgFromJson(str1);
    localEMMessage.setMsgId(paramCursor.getString(paramCursor.getColumnIndex("msgid")));
    localEMMessage.setMsgTime(paramCursor.getLong(paramCursor.getColumnIndex("msgtime")));
    int i1 = paramCursor.getInt(paramCursor.getColumnIndex("msgdir"));
    if (i1 == EMMessage.Direct.SEND.ordinal())
      localEMMessage.direct = EMMessage.Direct.SEND;
    else
      localEMMessage.direct = EMMessage.Direct.RECEIVE;
    int i2 = paramCursor.getInt(paramCursor.getColumnIndex("status"));
    if (i2 == EMMessage.Status.CREATE.ordinal())
      localEMMessage.status = EMMessage.Status.CREATE;
    else if (i2 == EMMessage.Status.INPROGRESS.ordinal())
      localEMMessage.status = EMMessage.Status.INPROGRESS;
    else if (i2 == EMMessage.Status.SUCCESS.ordinal())
      localEMMessage.status = EMMessage.Status.SUCCESS;
    else if (i2 == EMMessage.Status.FAIL.ordinal())
      localEMMessage.status = EMMessage.Status.FAIL;
    int i3 = paramCursor.getInt(paramCursor.getColumnIndex("isacked"));
    if (i3 == 0)
      localEMMessage.isAcked = false;
    else
      localEMMessage.isAcked = true;
    int i4 = paramCursor.getInt(paramCursor.getColumnIndex("isdelivered"));
    if (i4 == 0)
      localEMMessage.isDelivered = false;
    else
      localEMMessage.isDelivered = true;
    int i5 = paramCursor.getInt(paramCursor.getColumnIndex("islistened"));
    localEMMessage.setListened(i5 == 1);
    localEMMessage.setUnread(false);
    String str2 = paramCursor.getString(paramCursor.getColumnIndex("groupname"));
    if (str2 == null)
    {
      localEMMessage.setChatType(EMMessage.ChatType.Chat);
    }
    else
    {
      localEMMessage.setChatType(EMMessage.ChatType.GroupChat);
      localEMMessage.setTo(str2);
    }
    return localEMMessage;
  }

  public void f(String paramString)
  {
    try
    {
      SQLiteDatabase localSQLiteDatabase = a.getInstance(this.ab, this.aa).getWritableDatabase();
      int i1 = localSQLiteDatabase.delete("chat", "participant = ?", new String[] { paramString });
      EMLog.d(d, "delete chat msgs with:" + paramString + " return:" + i1);
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
  }

  public void b(String paramString, boolean paramBoolean)
  {
    try
    {
      SQLiteDatabase localSQLiteDatabase = a.getInstance(this.ab, this.aa).getWritableDatabase();
      EMLog.d(d, "add converstion with:" + paramString);
      String str = !paramBoolean ? "username" : "groupname";
      localSQLiteDatabase.execSQL("insert into conversation_list (" + str + ")" + " select ? where not exists (select null from " + "conversation_list" + " where " + str + " = ?)", new String[] { paramString, paramString });
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
  }

  void c(String paramString, boolean paramBoolean)
  {
    try
    {
      SQLiteDatabase localSQLiteDatabase = a.getInstance(this.ab, this.aa).getWritableDatabase();
      String str1 = !paramBoolean ? "participant" : "groupname";
      String str2 = !paramBoolean ? "username" : "groupname";
      localSQLiteDatabase.execSQL("delete from conversation_list where " + str2 + " = ? and " + " not exists (select null from " + "chat" + " where " + str1 + " = ?)", new String[] { paramString, paramString });
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
  }

  public void d(String paramString, boolean paramBoolean)
  {
    try
    {
      SQLiteDatabase localSQLiteDatabase = a.getInstance(this.ab, this.aa).getWritableDatabase();
      String str = !paramBoolean ? "username" : "groupname";
      localSQLiteDatabase.execSQL("delete from conversation_list where " + str + " = ?", new String[] { paramString });
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
  }

  public boolean e(String paramString, boolean paramBoolean)
  {
    try
    {
      SQLiteDatabase localSQLiteDatabase = a.getInstance(this.ab, this.aa).getWritableDatabase();
      Cursor localCursor = null;
      if (!paramBoolean)
        localCursor = localSQLiteDatabase.rawQuery("select count(*) from conversation_list where username = ? and groupname is null", new String[] { paramString });
      else
        localCursor = localSQLiteDatabase.rawQuery("select count(*) from conversation_list where username is null and groupname = ?", new String[] { paramString, "" });
      if (!localCursor.moveToFirst())
      {
        localCursor.close();
        return false;
      }
      int i1 = localCursor.getInt(0);
      EMLog.d(d, "has converstion:" + paramString + " isGroup:" + paramBoolean + " count:" + i1);
      localCursor.close();
      return i1 > 0;
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
    return false;
  }

  public void f(String paramString, boolean paramBoolean)
  {
    try
    {
      SQLiteDatabase localSQLiteDatabase = a.getInstance(this.ab, this.aa).getWritableDatabase();
      ContentValues localContentValues = new ContentValues();
      localContentValues.put("isacked", Boolean.valueOf(paramBoolean));
      localSQLiteDatabase.update("chat", localContentValues, "msgid = ?", new String[] { paramString });
      EMLog.d(d, "update msg:" + paramString + " ack:" + paramBoolean);
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
  }

  public void g(String paramString, boolean paramBoolean)
  {
    try
    {
      SQLiteDatabase localSQLiteDatabase = a.getInstance(this.ab, this.aa).getWritableDatabase();
      ContentValues localContentValues = new ContentValues();
      localContentValues.put("islistened", Boolean.valueOf(paramBoolean));
      localSQLiteDatabase.update("chat", localContentValues, "msgid = ?", new String[] { paramString });
      EMLog.d(d, "update msg:" + paramString + " isListened:" + paramBoolean);
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
  }

  public void h(String paramString, boolean paramBoolean)
  {
    try
    {
      SQLiteDatabase localSQLiteDatabase = a.getInstance(this.ab, this.aa).getWritableDatabase();
      ContentValues localContentValues = new ContentValues();
      localContentValues.put("isdelivered", Boolean.valueOf(paramBoolean));
      localSQLiteDatabase.update("chat", localContentValues, "msgid = ?", new String[] { paramString });
      EMLog.d(d, "update msg:" + paramString + " delivered:" + paramBoolean);
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
  }

  public void a(String paramString1, String paramString2)
  {
    ContentValues localContentValues = new ContentValues();
    localContentValues.put("msgid", paramString2);
    a().a(paramString1, localContentValues);
  }

  public void a(String paramString, ContentValues paramContentValues)
  {
    SQLiteDatabase localSQLiteDatabase = a.getInstance(this.ab, this.aa).getWritableDatabase();
    localSQLiteDatabase.update("chat", paramContentValues, "msgid = ?", new String[] { paramString });
  }

  private String c(List<String> paramList)
  {
    StringBuffer localStringBuffer = new StringBuffer();
    Iterator localIterator = paramList.iterator();
    while (localIterator.hasNext())
    {
      String str = (String)localIterator.next();
      localStringBuffer.append(str);
      localStringBuffer.append(",");
    }
    return localStringBuffer.toString();
  }

  public void a(EMGroup paramEMGroup)
  {
    try
    {
      SQLiteDatabase localSQLiteDatabase = a.getInstance(this.ab, this.aa).getWritableDatabase();
      ContentValues localContentValues = new ContentValues();
      localContentValues.put("name", paramEMGroup.getGroupId());
      localContentValues.put("jid", paramEMGroup.getEid());
      localContentValues.put("nick", paramEMGroup.getGroupName());
      localContentValues.put("desc", paramEMGroup.getDescription());
      localContentValues.put("owner", paramEMGroup.getOwner());
      List localList = paramEMGroup.getMembers();
      localContentValues.put("members", c(localList));
      localContentValues.put("members_size", Integer.valueOf(paramEMGroup.getAffiliationsCount()));
      localContentValues.put("modifiedtime", Long.valueOf(paramEMGroup.getLastModifiedTime()));
      localContentValues.put("ispublic", Boolean.valueOf(paramEMGroup.isPublic()));
      localContentValues.put("isblocked", Boolean.valueOf(paramEMGroup.isMsgBlocked()));
      localContentValues.put("max_users", Integer.valueOf(paramEMGroup.getMaxUsers()));
      localSQLiteDatabase.insert("emgroup", null, localContentValues);
      EMLog.d(d, "save group to db groupname:" + paramEMGroup.getGroupName());
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
  }

  public Map<String, EMGroup> f()
  {
    Hashtable localHashtable = new Hashtable();
    try
    {
      SQLiteDatabase localSQLiteDatabase = a.getInstance(this.ab, this.aa).getWritableDatabase();
      Cursor localCursor = localSQLiteDatabase.rawQuery("select * from emgroup", new String[0]);
      if (!localCursor.moveToFirst())
      {
        localCursor.close();
        return localHashtable;
      }
      do
      {
        EMGroup localEMGroup = c(localCursor);
        localHashtable.put(localEMGroup.getGroupId(), localEMGroup);
      }
      while (localCursor.moveToNext());
      localCursor.close();
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
    EMLog.d(d, "load groups from db:" + localHashtable.size());
    return localHashtable;
  }

  public EMGroup g(String paramString)
  {
    try
    {
      SQLiteDatabase localSQLiteDatabase = a.getInstance(this.ab, this.aa).getWritableDatabase();
      Cursor localCursor = localSQLiteDatabase.rawQuery("select * from emgroup where name  =?", new String[] { paramString });
      EMGroup localEMGroup = null;
      if (localCursor != null)
      {
        if (localCursor.moveToFirst())
          localEMGroup = c(localCursor);
        localCursor.close();
      }
      EMLog.d(d, "db load group:" + localEMGroup);
      return localEMGroup;
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
    return null;
  }

  private EMGroup c(Cursor paramCursor)
    throws Exception
  {
    String str1 = paramCursor.getString(paramCursor.getColumnIndex("name"));
    EMGroup localEMGroup = new EMGroup(str1);
    localEMGroup.setEid(paramCursor.getString(paramCursor.getColumnIndex("jid")));
    localEMGroup.setGroupName(paramCursor.getString(paramCursor.getColumnIndex("nick")));
    String str2 = paramCursor.getString(paramCursor.getColumnIndex("owner"));
    localEMGroup.setOwner(str2);
    localEMGroup.setLastModifiedTime(paramCursor.getLong(paramCursor.getColumnIndex("modifiedtime")));
    localEMGroup.setIsPublic(paramCursor.getInt(paramCursor.getColumnIndex("ispublic")) != 0);
    localEMGroup.setDescription(paramCursor.getString(paramCursor.getColumnIndex("desc")));
    localEMGroup.setAffiliationsCount(paramCursor.getInt(paramCursor.getColumnIndex("members_size")));
    localEMGroup.setMsgBlocked(paramCursor.getInt(paramCursor.getColumnIndex("isblocked")) != 0);
    localEMGroup.setMaxUsers(paramCursor.getInt(paramCursor.getColumnIndex("max_users")));
    String str3 = paramCursor.getString(paramCursor.getColumnIndex("members"));
    StringTokenizer localStringTokenizer = new StringTokenizer(str3, ",");
    while (localStringTokenizer.hasMoreTokens())
    {
      String str4 = localStringTokenizer.nextToken();
      localEMGroup.addMember(str4);
    }
    return localEMGroup;
  }

  public void b(EMGroup paramEMGroup)
  {
    try
    {
      SQLiteDatabase localSQLiteDatabase = a.getInstance(this.ab, this.aa).getWritableDatabase();
      ContentValues localContentValues = new ContentValues();
      localContentValues.put("jid", paramEMGroup.getEid());
      localContentValues.put("nick", paramEMGroup.getGroupName());
      localContentValues.put("desc", paramEMGroup.getDescription());
      localContentValues.put("owner", paramEMGroup.getOwner());
      List localList = paramEMGroup.getMembers();
      localContentValues.put("members", c(localList));
      localContentValues.put("members_size", Integer.valueOf(paramEMGroup.getAffiliationsCount()));
      localContentValues.put("modifiedtime", Long.valueOf(paramEMGroup.getLastModifiedTime()));
      localContentValues.put("ispublic", Boolean.valueOf(paramEMGroup.isPublic()));
      localContentValues.put("isblocked", Boolean.valueOf(paramEMGroup.isMsgBlocked()));
      localContentValues.put("max_users", Integer.valueOf(paramEMGroup.getMaxUsers()));
      localSQLiteDatabase.update("emgroup", localContentValues, "name = ?", new String[] { paramEMGroup.getGroupId() });
      EMLog.d(d, "updated group groupname:" + paramEMGroup.getGroupName());
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
  }

  public void h(String paramString)
  {
    try
    {
      SQLiteDatabase localSQLiteDatabase = a.getInstance(this.ab, this.aa).getWritableDatabase();
      int i1 = localSQLiteDatabase.delete("emgroup", "name = ?", new String[] { paramString });
      EMLog.d(d, "delete converstion with:" + paramString + " return:" + i1);
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
  }

  public void i(String paramString)
  {
    try
    {
      SQLiteDatabase localSQLiteDatabase = a.getInstance(this.ab, this.aa).getWritableDatabase();
      int i1 = localSQLiteDatabase.delete("chat", "groupname = ?", new String[] { paramString });
      EMLog.d(d, "delete group messages with:" + paramString + " return:" + i1);
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
  }

  public boolean c(EMMessage paramEMMessage)
  {
    paramEMMessage.status = EMMessage.Status.SUCCESS;
    paramEMMessage.isAcked = true;
    paramEMMessage.isDelivered = true;
    if (paramEMMessage.getMsgId() == null)
      paramEMMessage.setMsgId(Long.toString(System.currentTimeMillis()));
    if (paramEMMessage.getFrom() == null)
    {
      System.err.println("import msg error: msg from is null");
      return false;
    }
    if (paramEMMessage.getTo() == null)
    {
      System.err.println("import msg error: msg to is null");
      return false;
    }
    return a(paramEMMessage);
  }

  public synchronized void a(List<EMMessage> paramList)
  {
    HashMap localHashMap = new HashMap();
    Object localObject1 = paramList.iterator();
    while (((Iterator)localObject1).hasNext())
    {
      EMMessage localEMMessage1 = (EMMessage)((Iterator)localObject1).next();
      localHashMap.put(localEMMessage1.getMsgId(), localEMMessage1);
    }
    int i1 = 1000;
    localObject1 = a.getInstance(this.ab, this.aa).getWritableDatabase();
    while (paramList.size() > 0)
    {
      int i2 = 0;
      localObject2 = new StringBuilder();
      int i5 = 1;
      ((StringBuilder)localObject2).append("(");
      while ((paramList.size() > 0) && (i2 < 1000))
      {
        localObject3 = (EMMessage)paramList.remove(0);
        if (i5 != 0)
        {
          ((StringBuilder)localObject2).append("'" + ((EMMessage)localObject3).getMsgId() + "'");
          i5 = 0;
        }
        else
        {
          ((StringBuilder)localObject2).append(", '" + ((EMMessage)localObject3).getMsgId() + "'");
        }
        i2++;
      }
      ((StringBuilder)localObject2).append(")");
      Object localObject3 = "select msgid from chat where msgid in " + ((StringBuilder)localObject2).toString();
      Cursor localCursor = ((SQLiteDatabase)localObject1).rawQuery((String)localObject3, new String[0]);
      int i6 = 0;
      if (!localCursor.moveToFirst())
      {
        localCursor.close();
        i6 = 1;
      }
      if (i6 == 0)
      {
        do
        {
          String str = localCursor.getString(0);
          localHashMap.remove(str);
        }
        while (localCursor.moveToNext());
        localCursor.close();
      }
    }
    paramList.clear();
    Object localObject2 = localHashMap.values().iterator();
    while (((Iterator)localObject2).hasNext())
    {
      EMMessage localEMMessage2 = (EMMessage)((Iterator)localObject2).next();
      paramList.add(localEMMessage2);
    }
    int i3 = 300;
    while (paramList.size() > 0)
    {
      int i4 = 0;
      ((SQLiteDatabase)localObject1).beginTransaction();
      try
      {
        while ((paramList.size() > 0) && (i4 < 300))
        {
          EMMessage localEMMessage3 = (EMMessage)paramList.remove(0);
          if (localEMMessage3 != null)
          {
            a(localEMMessage3);
            i4++;
          }
        }
        ((SQLiteDatabase)localObject1).setTransactionSuccessful();
      }
      finally
      {
        ((SQLiteDatabase)localObject1).endTransaction();
      }
    }
  }

  public int j(String paramString)
  {
    try
    {
      SQLiteDatabase localSQLiteDatabase = a.getInstance(this.ab, this.aa).getReadableDatabase();
      int i1 = 0;
      Cursor localCursor = localSQLiteDatabase.rawQuery("select count from unreadcount where username = ?", new String[] { paramString });
      if (localCursor.moveToFirst())
        i1 = localCursor.getInt(localCursor.getColumnIndex("count"));
      if (i1 < 0)
        return 0;
      localCursor.close();
      return i1;
    }
    catch (Exception localException)
    {
    }
    return 0;
  }

  public void a(String paramString, int paramInt)
  {
    try
    {
      SQLiteDatabase localSQLiteDatabase = a.getInstance(this.ab, this.aa).getWritableDatabase();
      ContentValues localContentValues = new ContentValues();
      localContentValues.put("username", paramString);
      localContentValues.put("count", Integer.valueOf(paramInt));
      localSQLiteDatabase.replace("unreadcount", null, localContentValues);
    }
    catch (Exception localException)
    {
    }
  }

  public void k(String paramString)
  {
    try
    {
      SQLiteDatabase localSQLiteDatabase = a.getInstance(this.ab, this.aa).getWritableDatabase();
      localSQLiteDatabase.delete("unreadcount", "username = ?", new String[] { paramString });
    }
    catch (Exception localException)
    {
    }
  }

  public List<String> g()
  {
    ArrayList localArrayList = new ArrayList();
    try
    {
      SQLiteDatabase localSQLiteDatabase = a.getInstance(this.ab, this.aa).getReadableDatabase();
      Cursor localCursor = localSQLiteDatabase.rawQuery("select * from unreadcount", new String[0]);
      if (!localCursor.moveToFirst())
      {
        localCursor.close();
        return localArrayList;
      }
      do
      {
        String str = localCursor.getString(0);
        int i1 = localCursor.getInt(1);
        if (i1 > 0)
          localArrayList.add(str);
      }
      while (localCursor.moveToNext());
      localCursor.close();
    }
    catch (Exception localException)
    {
    }
    return localArrayList;
  }

  public void a(String paramString, k.b paramb)
  {
    try
    {
      SQLiteDatabase localSQLiteDatabase = a.getInstance(this.ab, this.aa).getWritableDatabase();
      ContentValues localContentValues = new ContentValues();
      localContentValues.put("username", paramString);
      localContentValues.put("value", paramb.a());
      localContentValues.put("saved_time", Long.valueOf(paramb.b()));
      localSQLiteDatabase.replace("token", null, localContentValues);
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
  }

  public k.b l(String paramString)
  {
    try
    {
      SQLiteDatabase localSQLiteDatabase = a.getInstance(this.ab, this.aa).getReadableDatabase();
      k.b localb = null;
      Cursor localCursor = localSQLiteDatabase.rawQuery("select * from token where username = ?", new String[] { paramString });
      if (localCursor != null)
      {
        localb = new k.b();
        if (localCursor.moveToFirst())
        {
          String str = localCursor.getString(localCursor.getColumnIndex("value"));
          long l1 = localCursor.getLong(localCursor.getColumnIndex("saved_time"));
          if (str != null)
            localb.a(str);
          localb.a(l1);
          localCursor.close();
        }
        else
        {
          localCursor.close();
        }
      }
      return localb;
    }
    catch (Exception localException)
    {
    }
    return null;
  }

  public void b(String paramString1, String paramString2)
  {
    EMLog.d(d, "add contact to db:" + paramString2);
    try
    {
      SQLiteDatabase localSQLiteDatabase = a.getInstance(this.ab, this.aa).getWritableDatabase();
      ContentValues localContentValues = new ContentValues();
      localContentValues.put("jid", paramString1);
      localContentValues.put("username", paramString2);
      localSQLiteDatabase.replace("contact", null, localContentValues);
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
  }

  public void m(String paramString)
  {
    try
    {
      SQLiteDatabase localSQLiteDatabase = a.getInstance(this.ab, this.aa).getWritableDatabase();
      localSQLiteDatabase.delete("contact", "jid = ?", new String[] { paramString });
      EMLog.d(d, "delete contact jid:" + paramString);
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
  }

  public List<EMContact> h()
  {
    LinkedList localLinkedList = new LinkedList();
    try
    {
      SQLiteDatabase localSQLiteDatabase = a.getInstance(this.ab, this.aa).getWritableDatabase();
      Cursor localCursor = localSQLiteDatabase.rawQuery("select * from contact", new String[0]);
      if (!localCursor.moveToFirst())
      {
        localCursor.close();
        return localLinkedList;
      }
      do
      {
        String str1 = localCursor.getString(0);
        String str2 = localCursor.getString(1);
        EMContact localEMContact = new EMContact(str1, str2);
        localLinkedList.add(localEMContact);
      }
      while (localCursor.moveToNext());
      localCursor.close();
      EMLog.d(d, "loaded contacts from db:" + localLinkedList.size());
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
    return localLinkedList;
  }

  public List<String> i()
  {
    SQLiteDatabase localSQLiteDatabase = a.getInstance(this.ab, this.aa).getReadableDatabase();
    Cursor localCursor = localSQLiteDatabase.rawQuery("select * from black_list", new String[0]);
    ArrayList localArrayList = new ArrayList();
    if (!localCursor.moveToFirst())
    {
      localCursor.close();
      return localArrayList;
    }
    do
    {
      String str = localCursor.getString(localCursor.getColumnIndex("username"));
      localArrayList.add(str);
    }
    while (localCursor.moveToNext());
    localCursor.close();
    return localArrayList;
  }

  public void b(List<String> paramList)
  {
    SQLiteDatabase localSQLiteDatabase = a.getInstance(this.ab, this.aa).getWritableDatabase();
    try
    {
      if (localSQLiteDatabase.isOpen())
      {
        localSQLiteDatabase.execSQL("delete from black_list");
        Iterator localIterator = paramList.iterator();
        while (localIterator.hasNext())
        {
          String str = (String)localIterator.next();
          ContentValues localContentValues = new ContentValues();
          localContentValues.put("username", str);
          localSQLiteDatabase.insert("black_list", null, localContentValues);
        }
      }
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
  }

  public void n(String paramString)
  {
    try
    {
      SQLiteDatabase localSQLiteDatabase = a.getInstance(this.ab, this.aa).getWritableDatabase();
      if (localSQLiteDatabase.isOpen())
        localSQLiteDatabase.delete("black_list", "username = ?", new String[] { paramString });
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
  }

  public void o(String paramString)
  {
    try
    {
      SQLiteDatabase localSQLiteDatabase = a.getInstance(this.ab, this.aa).getWritableDatabase();
      if (localSQLiteDatabase.isOpen())
      {
        ContentValues localContentValues = new ContentValues();
        localContentValues.put("username", paramString);
        localSQLiteDatabase.insert("black_list", null, localContentValues);
      }
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
  }

  public void a(String paramString1, boolean paramBoolean, String paramString2)
  {
    SQLiteDatabase localSQLiteDatabase = a.getInstance(this.ab, this.aa).getReadableDatabase();
    try
    {
      ContentValues localContentValues = new ContentValues();
      localContentValues.put("ext", paramString2 == null ? "" : paramString2);
      String str = !paramBoolean ? "username" : "groupname";
      localSQLiteDatabase.update("conversation_list", localContentValues, str + " = ?", new String[] { paramString1 });
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
  }

  public String i(String paramString, boolean paramBoolean)
  {
    SQLiteDatabase localSQLiteDatabase = a.getInstance(this.ab, this.aa).getReadableDatabase();
    try
    {
      String str1 = !paramBoolean ? "username" : "groupname";
      Cursor localCursor = localSQLiteDatabase.rawQuery("select ext from conversation_list where " + str1 + " = ?", new String[] { paramString });
      if (!localCursor.moveToFirst())
      {
        localCursor.close();
        return "";
      }
      String str2 = localCursor.getString(0);
      localCursor.close();
      return str2;
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
    return "";
  }

  public boolean j()
  {
    return this.ac;
  }

  private static class a extends SQLiteOpenHelper
  {
    private static a instance = null;
    private String username;

    private a(Context paramContext, String paramString)
    {
      super(paramString + "_emmsg.db", null, 10);
      this.username = paramString;
      EMLog.d(e.k(), "created chatdb for :" + paramString);
    }

    public static synchronized a getInstance(Context paramContext, String paramString)
    {
      if (instance == null)
        instance = new a(paramContext, paramString);
      return instance;
    }

    public static synchronized void closeDB()
    {
      if (instance != null)
      {
        try
        {
          SQLiteDatabase localSQLiteDatabase = instance.getWritableDatabase();
          localSQLiteDatabase.close();
        }
        catch (Exception localException)
        {
          localException.printStackTrace();
        }
        instance = null;
      }
    }

    public void onCreate(SQLiteDatabase paramSQLiteDatabase)
    {
      paramSQLiteDatabase.execSQL("create table chat (_id integer primary key autoincrement, msgid text, msgtime integer, msgdir integer, isacked integer, isdelivered integer, status integer,participant text not null, islistened integer, msgbody text not null,groupname text);");
      paramSQLiteDatabase.execSQL("create table emgroup (name text primary key, jid text not null, nick text not null, owner text not null, modifiedtime integer, ispublic integer, desc text, members_size integer, isblocked integer, members text, max_users integer);");
      paramSQLiteDatabase.execSQL("create table unreadcount (username text primary key, count integer);");
      paramSQLiteDatabase.execSQL("create table token (username text primary key, value text, saved_time integer);");
      paramSQLiteDatabase.execSQL("create table contact (jid text primary key, username text, nick );");
      paramSQLiteDatabase.execSQL("create table black_list (username text primary key);");
      paramSQLiteDatabase.execSQL("create table if not exists conversation_list (username text primary key, groupname text, ext text);");
    }

    public void onUpgrade(SQLiteDatabase paramSQLiteDatabase, int paramInt1, int paramInt2)
    {
      Log.w(e.k(), "Upgrading from version " + paramInt1 + " to " + paramInt2 + ", which will destroy all old data");
      if (paramInt1 < 2)
        paramSQLiteDatabase.execSQL("create table unreadcount (username text primary key, count integer);");
      if (paramInt1 < 3)
      {
        paramSQLiteDatabase.execSQL("create table token (username text primary key, value text, saved_time integer);");
        paramSQLiteDatabase.execSQL("create table contact (jid text primary key, username text, nick );");
      }
      if (paramInt1 < 4)
        try
        {
          paramSQLiteDatabase.delete("token", "username = ?", new String[] { this.username });
        }
        catch (Exception localException1)
        {
          localException1.printStackTrace();
        }
      if (paramInt1 < 5)
        try
        {
          paramSQLiteDatabase.execSQL("ALTER TABLE chat ADD COLUMN isdelivered integer ;");
          EMLog.d(e.k(), "db upgrade to vervison 5");
        }
        catch (Exception localException2)
        {
          localException2.printStackTrace();
        }
      if (paramInt1 < 6)
      {
        paramSQLiteDatabase.execSQL("create table black_list (username text primary key);");
        paramSQLiteDatabase.execSQL("ALTER TABLE chat ADD COLUMN islistened integer ;");
      }
      if (paramInt1 < 7)
        paramSQLiteDatabase.execSQL("ALTER TABLE emgroup ADD COLUMN members_size INTEGER ;");
      if (paramInt1 < 8)
        paramSQLiteDatabase.execSQL("ALTER TABLE emgroup ADD COLUMN isblocked INTEGER ;");
      if (paramInt1 < 9)
        paramSQLiteDatabase.execSQL("ALTER TABLE emgroup ADD COLUMN max_users INTEGER ;");
      if (paramInt1 < 10)
      {
        paramSQLiteDatabase.execSQL("create table if not exists conversation_list (username text primary key, groupname text, ext text);");
        initializeConversation(paramSQLiteDatabase);
        clearContactTable_v10(paramSQLiteDatabase);
        e.a(e.a(), true);
      }
    }

    private void clearContactTable_v10(SQLiteDatabase paramSQLiteDatabase)
    {
      try
      {
        EMLog.d(e.k(), "add converstion with:" + this.username);
        paramSQLiteDatabase.execSQL("delete from contact", new String[0]);
      }
      catch (Exception localException)
      {
        localException.printStackTrace();
      }
    }

    private void initializeConversation(SQLiteDatabase paramSQLiteDatabase)
    {
      EMLog.d(e.k(), "initializeConversation");
      ArrayList localArrayList1 = new ArrayList();
      ArrayList localArrayList2 = new ArrayList();
      try
      {
        Cursor localCursor = paramSQLiteDatabase.rawQuery("select distinct participant from chat where groupname is null", null);
        if (localCursor.moveToFirst())
          do
            localArrayList1.add(localCursor.getString(0));
          while (localCursor.moveToNext());
        localCursor.close();
        localCursor = paramSQLiteDatabase.rawQuery("select distinct groupname from chat where groupname is not null", null);
        if (localCursor.moveToFirst())
          do
            localArrayList2.add(localCursor.getString(0));
          while (localCursor.moveToNext());
        localCursor.close();
        EMLog.d(e.k(), "load participants size:" + localArrayList1.size());
      }
      catch (Exception localException)
      {
        localException.printStackTrace();
      }
      Iterator localIterator = localArrayList1.iterator();
      String str;
      while (localIterator.hasNext())
      {
        str = (String)localIterator.next();
        addConversation(paramSQLiteDatabase, str, false);
      }
      localIterator = localArrayList2.iterator();
      while (localIterator.hasNext())
      {
        str = (String)localIterator.next();
        addConversation(paramSQLiteDatabase, str, true);
      }
    }

    private void addConversation(SQLiteDatabase paramSQLiteDatabase, String paramString, boolean paramBoolean)
    {
      try
      {
        EMLog.d(e.k(), "add converstion with:" + paramString);
        String str = !paramBoolean ? "username" : "groupname";
        paramSQLiteDatabase.execSQL("insert into conversation_list (" + str + ")" + " select ? where not exists (select null from " + "conversation_list" + " where " + str + " = ?)", new String[] { paramString, paramString });
      }
      catch (Exception localException)
      {
        localException.printStackTrace();
      }
    }
  }
}

/* Location:           D:\hygieia\userclient\common\libs\easemobchat_2.1.8.jar
 * Qualified Name:     com.easemob.chat.core.e
 * JD-Core Version:    0.6.1
 */